'use strict';

const Sequelize = require('sequelize');
const sequelize = require('../plugins/db');
const Category = require('./category.model')
const Model = Sequelize.Model;
class Content extends Model {}

// 静态属性：主键字段名称
Content.pk = 'id';
Content.init({
    title: Sequelize.STRING,
    content: Sequelize.TEXT('long'),
    source: Sequelize.STRING,
    author: Sequelize.STRING,
    sub_title: Sequelize.STRING,
    intro: Sequelize.STRING,
    cover: Sequelize.STRING,
    images: {
        type: Sequelize.TEXT,
        allowNull: true,
        set: function(val) {
            if (val){
                this.setDataValue('images', val.join(','));
                this.setDataValue('cover', val[0]);
            }else {
                return null;
            }
        },
        get() {
            const images = this.getDataValue('images');
            return images ? images.split(',') : null
        }
    },
    redirect_url: Sequelize.STRING,
    redirect: {
        type: Sequelize.ENUM('yes', 'no'),
        allowNull: false,
        defaultValue: 'no'
    },
    // category: Sequelize.STRING,
    hits: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    top: {
        type: Sequelize.ENUM('yes', 'no'),
        allowNull: false,
        defaultValue: 'no'
    },
    position: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    author_id: Sequelize.INTEGER,
    publish_time: {
        type: Sequelize.DATE,
        allowNull: true,
        get() {
            const date = this.getDataValue('publish_time');
            return date ? date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN',{hour12: false, timeStyle: 'short'}) : null
        }
    },
    goods: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    area: Sequelize.STRING,
    status: {
        type: Sequelize.ENUM('published', 'draft', 'delete'),
        allowNull: false,
        defaultValue: 'draft'
    },
    tags: {
        type: Sequelize.STRING,
        get() {
            const tags = this.getDataValue('tags');
            return tags ? tags.split(',') : [];
        },
        set(val) {
            this.setDataValue('tags', val.join(','));
        }
    },
    comment: {
        type: Sequelize.ENUM('allow', 'disallow'),
        allowNull: false,
        defaultValue: 'disallow'
    },
    createdAt: {
        type: Sequelize.DATE,
        get() {
            const date = this.getDataValue('createdAt');
            return date ? date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN',{hour12: false, timeStyle: 'short'}) : null
        }
    },
    updatedAt: {
        type: Sequelize.DATE,
        get() {
            const date = this.getDataValue('createdAt');
            return date ? date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN',{hour12: false, timeStyle: 'short'}) : null
        }
    },
    deletedAt: {
        type: Sequelize.DATE,
        get() {
            const date = this.getDataValue('deletedAt');
            return date ? date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN',{hour12: false, timeStyle: 'short'}) : null
        }
    }
}, {
    sequelize,
    tableName: 'content',
    freezeTableName: true,
    timestamps: true,
    paranoid: true
    // createdAt: 'create_time',
    // updatedAt: 'update_time',
});
Content.belongsTo(Category, {
    as: 'categoryinfo',
    targetKey: 'cid',
    foreignKey: 'category'
})
module.exports = Content;
